package com.pkutech.carcasher.activity;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.dd.processbutton.iml.ActionProcessButton;
import com.pkutech.carcasher.R;
import com.pkutech.carcasher.bean.RegisterInfo;
import com.pkutech.carcasher.bean.RegisterResponse;
import com.pkutech.carcasher.network.SoapApi;
import com.pkutech.carcasher.utils.ToastUtil;
import com.rengwuxian.materialedittext.MaterialEditText;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.schedulers.Schedulers;

/**
 * 注册
 * Created by cyy on 2016/6/6.
 */
public class RegisterActivity extends BaseActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.pwdEditText)
    MaterialEditText pwdEditText;
    @BindView(R.id.pwdCheckBox)
    CheckBox pwdCheckBox;
    @BindView(R.id.pwdAgainEditText)
    MaterialEditText pwdAgainEditText;
    @BindView(R.id.pwdAaginCheckBox)
    CheckBox pwdAgainCheckBox;
    @BindView(R.id.userNameEditText)
    MaterialEditText userNameEditText;
    @BindView(R.id.phoneNumEditText)
    MaterialEditText phoneNumEditText;
    @BindView(R.id.carNumEditText)
    MaterialEditText carNumEditText;
    @BindView(R.id.registerBtn)
    ActionProcessButton registerBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        initToolbar(toolbar);
        pwdCheckBox = (CheckBox) findViewById(R.id.pwdCheckBox);
        pwdCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    // 设置EditText的密码为可见的
                    pwdEditText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    pwdEditText.setSelection(pwdEditText.getText().toString().length());
                } else {
                    // 设置密码为隐藏的
                    pwdEditText.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    pwdEditText.setSelection(pwdEditText.getText().toString().length());
                }
            }
        });
        pwdAgainCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    // 设置EditText的密码为可见的
                    pwdAgainEditText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    pwdAgainEditText.setSelection(pwdAgainEditText.getText().toString().length());
                } else {
                    // 设置密码为隐藏的
                    pwdEditText.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    pwdAgainEditText.setSelection(pwdAgainEditText.getText().toString().length());
                }
            }
        });
    }

    @OnClick(R.id.registerBtn)
    public void register() {
        RegisterInfo registerInfo = new RegisterInfo();
        registerInfo.setAccount(userNameEditText.getText().toString());
        registerInfo.setPassword(pwdEditText.getText().toString());
        registerInfo.setName("ccc");
        registerInfo.setPhone(phoneNumEditText.getText().toString());
        registerInfo.setCarnum(carNumEditText.getText().toString());
        registerInfo.setSex("M");
        SoapApi.register(registerInfo)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        registerBtn.setProgress(1);
                    }
                })
                .subscribe(new Subscriber<RegisterResponse>() {
                    @Override
                    public void onCompleted() {
                        registerBtn.setProgress(0);
                    }

                    @Override
                    public void onError(Throwable e) {
                        registerBtn.setProgress(0);
                        ToastUtil.showInfoToast(RegisterActivity.this, "登陆失败", ToastUtil.Position.TOP);
                    }

                    @Override
                    public void onNext(RegisterResponse registerResponse) {
                        registerBtn.setProgress(0);
                        if(registerResponse != null && !TextUtils.isEmpty(registerResponse.getMessage())){
                            if("用户注册成功!".equals(registerResponse.getMessage())){
                                ToastUtil.showInfoToast(RegisterActivity.this, "注册成功", ToastUtil.Position.TOP);
                                finish();
                            }else{
                                ToastUtil.showInfoToast(RegisterActivity.this, registerResponse.getMessage(), ToastUtil.Position.TOP);
                            }
                        }else{
                            ToastUtil.showInfoToast(RegisterActivity.this, "注册失败", ToastUtil.Position.TOP);
                        }
                    }
                });
    }
}
